package com.njmaomao.gj.pyot.controller;

import com.njmaomao.gj.pyot.common.ResponseCode;
import com.njmaomao.gj.pyot.common.bean.BaseRequest;
import com.njmaomao.gj.pyot.common.bean.Response;
import com.njmaomao.gj.pyot.common.bean.dto.*;
import com.njmaomao.gj.pyot.service.WechatAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Api(description = "微信支付")
@RestController
@RequestMapping("/wechat")
public class WechatPayController {


    @Autowired
    private WechatAppService wechatAppService;

    @ApiOperation(value = "微信jsapi支付", notes = "微信jsapi支付")
    @RequestMapping(value = "/js-pay", method = {RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response<WechatJsPayResponse> jsPay(@RequestBody @Valid OrderPayReqDTO req) {
        return response(req, wechatAppService.jsPay(req.getIsSandbox(),
                req.getAppId(),
                req.getOrderCode(),
                req.getOrderType(),
                req.getTotalFee(),
                req.getOpenId(),
                req.getFromSystem(),
                req.getCallBackUrl(),
                req.getOrderDesc(),
                req.getCustomerId()));
    }

    @ApiOperation(value = "查询订单支付", notes = "查询订单支付")
    @RequestMapping(value = "/orderQuery", method = {RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response<OrderQueryRespDTO> queryOrder(@RequestBody @Valid OrderQueryReqDTO req) {
        OrderQueryRespDTO orderQueryRespDTO = wechatAppService.queryOrder(req.getIsSandbox(), req.getOrderCode());
        return response(req, orderQueryRespDTO);
    }

    @ApiOperation(value = "申请退款", notes = "申请退款")
    @RequestMapping(value = "/refund", method = {RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response<RefundRespDTO> refund(@RequestBody @Valid RefundReqDTO req) {
        RefundRespDTO refundRespDTO = wechatAppService.refundOrder(req.getIsSandbox(), req.getTradeNo(), req.getRefundOrderCode(), req.getRefundFee(), req.getOrderCode(), String.valueOf(req.getOperator()), req.getCallBackUrl(), req.getFromSystem(), req.getRefundDesc());
        return response(req, refundRespDTO);
    }

    @ApiOperation(value = "查询退款", notes = "查询退款")
    @RequestMapping(value = "/refundQuery", method = {RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response<RefundQueryRespDTO> refundQuery(@RequestBody @Valid RefundQueryReqDTO req) {
        RefundQueryRespDTO refundQueryRespDTO = wechatAppService.refundQuery(req.getIsSandbox(), req.getRefundOrderCode());
        return response(req, refundQueryRespDTO);
    }

    @ApiOperation(value = "查询用户已支付的订单", notes = "查询用户已支付的订单")
    @RequestMapping(value = "/customer-paid-order", method = {RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response<CustomerPaidOrderRespDTO> customerPaidOrder(@RequestBody @Valid CustomerPaidOrderReqDTO req) {
        CustomerPaidOrderRespDTO customerPaidOrderRespDTO = wechatAppService.customerPaidOrder(
                req.getCustomerId()
        );
        return Response.success(req, customerPaidOrderRespDTO);
    }

    private <R extends WechatResponse> Response<R> response (BaseRequest req, R wechatResponse) {
        return wechatResponse.isResultSuccess()
                ? Response.success(req, wechatResponse)
                : Response.fail(req.getSeqNo(), ResponseCode.PAYTO.WECHAT_BIZ_FAIL, wechatResponse);
    }

}
